/*

File name		: figure3b.do

Description		: Donation rates by experimental conditions
								
Output File		: figure3b.png
				
*/


set more off, perm
capture restore

*** 001 Plot Descriptive Graph ***
use "data/blood_donation/blood_donation.dta", clear

	// Merge rainfall data
	merge m:1 entnahmedatum using "data/rainfall/rainfallZH.dta"

	// Rename kalendertag daily_rainfall
	rename daily_rainfall_ZH daily_rainfall
	gen any_rain=1 if daily_rainfall>0&daily_rainfall!=.
	replace any_rain=0 if daily_rainfall==0
	sort spnr entnahmedatum
	gen any_rain_plus1=any_rain[_n+1] if spnr==spnr[_n+1]

	sort spnr period
	sum don if period==1
	sum don if period==2&don[_n-1]==1&spnr==spnr[_n-1]
	sum don if period==2&don[_n-1]==0&spnr==spnr[_n-1]
	sum don if period==3&don[_n-1]==1&spnr==spnr[_n-1]
	sum don if period==3&don[_n-1]==0&spnr==spnr[_n-1]
	sum don if period==4&don[_n-1]==1&spnr==spnr[_n-1]
	sum don if period==4&don[_n-1]==0&spnr==spnr[_n-1]

	// Generate controls
	gen spring=1 if period==1|period==3
	replace spring=0 if spring==.
	gen month=month(entnahmedatum)
	gen week=week(entnahmedatum)
	tab month, gen(month_dummy)
	tab c_num, gen(c_num_dummy)

	label define order 1 "K" 2 "1a" 3 "1b" 4 "2"
	encode treatment, gen(treatment_ordered) label(order)
	label values treatment_ordered order

	/* Gen new variable that is 1=donated in period 1, 2=did not donate in 
	   period 1, 3=donated in period2, 4=did not donate in period 2 */
	sort spnr period
	gen treatment_bydonation1=1 if period==1 & don==1
	replace treatment_bydonation1=0 if period==1 & don==0
	replace treatment_bydonation1=treatment_bydonation1[_n-1] if spnr==spnr[_n-1]
	gen treatment_bydonation2=1 if period==2 & don==1
	replace treatment_bydonation2=0 if period==2 & don==0
	replace treatment_bydonation2=treatment_bydonation2[_n-1] ///
	if spnr==spnr[_n-1]&treatment_bydonation2[_n-1]!=.
	replace treatment_bydonation2=treatment_bydonation2[_n+1] ///
	if spnr==spnr[_n+1]&treatment_bydonation2[_n+1]!=.

	collapse (mean) meandon = don (sd) sddon=don (count) n=don, by ( treatment_ordered period )
	
	
	//SE
	generate hidon = meandon + (sddon / sqrt(n))
	generate lowdon = meandon - (sddon / sqrt(n))

	generate periodtreatment = treatment_ordered    if period == 1
	replace  periodtreatment = treatment_ordered+5  if period == 2
	replace  periodtreatment = treatment_ordered+10 if period == 3
	replace  periodtreatment = treatment_ordered+15 if period == 4

	sort periodtreatment
	list periodtreatment period treatment_ordered, sepby(period)

	gen don_graph=meandon if period>1
	replace don_graph=(meandon[1]+meandon[3])*0.5 ///
	if period==1&(treatment_ordered==1|treatment_ordered==3)
	replace don_graph=(meandon[2]+meandon[4])*0.5 ///
	if period==1&(treatment_ordered==2|treatment_ordered==4)

	// Sum of standard deviations for combined period 1 graph
	gen sddon_sum=sqrt(sddon[1]^2+sddon[3]^2) ///
	if period==1&(treatment_ordered==1|treatment_ordered==3)
	replace sddon_sum=sqrt(sddon[2]^2+sddon[4]^2) ///
	if period==1&(treatment_ordered==2|treatment_ordered==4)
	
	gen n_sum=n[1]+n[3] if period==1&(treatment_ordered==1|treatment_ordered==3)
	replace n_sum=n[2]+n[4] ///
	if period==1&(treatment_ordered==2|treatment_ordered==4)
	
	gen hidon_graph=hidon if period>1
	replace hidon_graph=don_graph+ (sddon_sum / sqrt(n_sum)) if period==1
	
	gen lowdon_graph=lowdon if period>1
	replace lowdon_graph=don_graph- (sddon_sum / sqrt(n_sum)) if period==1

	generate periodtreatment2 = 1.1 if period == 1
	replace  periodtreatment2 = treatment_ordered*0.05+2  if period == 2
	replace  periodtreatment2 = treatment_ordered*0.05+3 if period == 3
	replace  periodtreatment2 = treatment_ordered*0.05+4 if period == 4

	// Plot line graph
	*levelsof treatment_ordered, local(levels)
	local i=0
	local ml "D S O T"
	local l "solid longdash dash shortdash"
	local gs "gs13 black gs7 gs10"
	forvalues i = 1(1)4 {
		
		local gr `gr' rcap hidon_graph lowdon_graph periodtreatment2 ///
		if treatment_ordered == `i', lcolor("`: word `i' of `gs''") ///
		|| connected don_graph periodtreatment2 if treatment_ordered == `i', ///
		lpattern("`: word `i' of `l''") lwidth(thin) mfcolor(white) msize(vlarge) ///
		msymbol("`: word `i' of `ml''") mcolor("`: word `i' of `gs''") ///
		lcolor("`: word `i' of `gs''")||
		
		*graph twoway `gr' , legend(nobox region(style(none)) ///
		pos(2) ring(0) col(1) order(2 "Control" 4 "Treatment C1" ///
		6 "Treatment C2" 8 "Treatment C12")) graphregion(color(white)) ///
		bgcolor(white) ylabel(0.1(0.05)0.3) xtitle("") ytitle("Donation rate") ///
		xscale(range(0.8 4.4)) xlabel(1.1 "Period 1" 2.1 "Period 2" ///
		3.1 "Period 3" 4.1 "Period 4", noticks)
		

		}
		
	graph twoway `gr' , legend(nobox region(style(none)) size(medium) pos(2) ring(0) col(1) ///
	order(4 "Condition C1" 6 "Condition C2" 8 "Condition C12" 2 "Control")) ///
	graphregion(color(white)) bgcolor(white) ylabel(0.1(0.05)0.3, labsize(medium)) ///
	xtitle("") ytitle("Donation rate", size(medium)) xscale(range(0.8 4.4)) ///
	xlabel(1.1 "Period 1" 2.1 "Period 2" 3.1 "Period 3" 4.1 "Period 4", noticks labsize(medium))
	
	
	graph export output/figures/figure3b.png, replace width(2048)	

